package com.his.dictorderfreq.service.Imp;

import com.his.dictorderfreq.dao.DictOrderFreqMapper;
import com.his.dictorderfreq.entity.DictOrderFreq;
import com.his.dictorderfreq.service.DictOrderFreqService;
import com.his.dictorderfreq.vo.OrderFreqSaveVo;
import com.his.dictorderfreq.vo.OrderFreqVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zhoufei
 * @Date 2020/3/9
 */
@Service
public class DictOrderFreqServiceImpl implements DictOrderFreqService
{
    @Autowired
    private  DictOrderFreqMapper dictOrderFreqMapper;

    @Override
    public void insert(OrderFreqVo orderFreqVo) {
        DictOrderFreq orderFreq = BeanUtil.copyBean(orderFreqVo,DictOrderFreq.class);
        dictOrderFreqMapper.insertAllColumn(orderFreq);
    }
    @Override
    public void deleteByKey(OrderFreqVo orderFreqVo) {
        DictOrderFreq orderFreq = BeanUtil.copyBean(orderFreqVo,DictOrderFreq.class);
        dictOrderFreqMapper.deleteById(orderFreq);
    }
    @Override
    public List<DictOrderFreq> queryAll(DictEmployee dictEmployee){
        DictOrderFreq orderFreq = new DictOrderFreq();
        orderFreq.setHOSPID(dictEmployee.getHOSPID());
        List<DictOrderFreq> dictOrderFreq = dictOrderFreqMapper.selectByEntity(orderFreq);
        return dictOrderFreq;
    }

    @Override
    public void updateById(OrderFreqVo orderFreqVo){
        DictOrderFreq dictOrderFreq = BeanUtil.copyBean(orderFreqVo,DictOrderFreq.class);
        dictOrderFreqMapper.updateById(dictOrderFreq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savAll(DictEmployee dictEmployee, OrderFreqSaveVo orderFreqSaveVo) {
        String hospId = dictEmployee.getHOSPID();
        if(!StringUtils.isEmpty(orderFreqSaveVo.getHospid())){
            hospId = orderFreqSaveVo.getHospid();
        }
        dictOrderFreqMapper.deleteByHospId(hospId);
        for(OrderFreqVo orderFreqVo : orderFreqSaveVo.getOrderFreqVoList()){
            DictOrderFreq orderFreq = BeanUtil.copyBean(orderFreqVo,DictOrderFreq.class);
            if(StringUtils.isEmpty(orderFreq.getHOSPID())){
                orderFreq.setHOSPID(dictEmployee.getHOSPID());
            }
            dictOrderFreqMapper.insertAllColumn(orderFreq);
        }
    }

    @Override
    public DictOrderFreq selectByFreqId(DictEmployee dictEmployee, String freqId) {
        DictOrderFreq dictOrderFreq = new DictOrderFreq();
        dictOrderFreq.setHOSPID(dictEmployee.getHOSPID());
        dictOrderFreq.setFREQID(freqId);
        List<DictOrderFreq> list = dictOrderFreqMapper.selectByEntity(dictOrderFreq);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
